<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/20
 * Time: 11:57
 */

namespace app\index\controller;


use Protocol\Curl;
use Redis\Redis;
use Sms\Sms;
use think\Controller;
use think\Db;
use think\Request;

class SmsCode extends Controller
{
    const APP_ID = 'd291296b4836fbd653f61279b387b91f'; // appid 请更换为您自己的应用appId
    const API_KEY = '36e92cfbd6a642d4b1ea8d59602627a2'; // appKey 请更换为您自己的应用appKey

    function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * @param $phone
     * @return array
     * @content 焦云发送短信验证码
     */
    function smsCode($phone)
    {
        if (!empty($phone)) {
            $redis = new Redis();
            $_code = $redis->hGet('smsCode', strval($phone));
            $mark = false;
            if (!empty($_code)) {
                $code = $_code;
            } else {
                $mark = true;
                $code = randCodes();
            }
            $sms = new Sms(self::APP_ID, self::API_KEY);
            #验证码短信模板id
            $tpl_id = '2831';
            #模板变量
            $tpl_value = "#code#=" . $code;
            $t = $sms->sendSms($phone, $tpl_id, $tpl_value);
            var_dump($t);
            if ($t['code'] == '100') {
                # 发送成功,存入redis
                if ($mark)
                    $redis->hSet('smsCode', strval($phone), strval($code), '1800');
                $biz_id = Db::table('biz')->field('id')->where(array('biz_phone' => $phone, 'biz_status' => 1))->find();
                # 添加短信发送记录
                Db::table('log_sms')->data(array('biz_id' => $biz_id['id'], 'phone' => $phone, 'sms_create' => date('Y-m-d H:i:s'), 'interface' => 2, 'content' => $code))->insert();
                return array('status' => true, 'code' => $code);
            } else {
                return array('status' => false, 'msg' => '发送失败');
            }
        } else {
            return array('status' => false, 'msg' => '手机号为空');
        }
    }

    /**
     * @param null $mobile 手机号
     * @param null $content 发送内容(发送短信验证码时不用传)
     * @param bool $mark true 为发送验证码 , false为发送通知
     * @return array
     */
    function Ysms($mobile = null, $content = null, $mark = true, $bizID = '')
    {
        if (!empty($mobile)) {
            $_status = false;
            $redis = new Redis();
            if ($mark) {
                # 发送短信验证码
                $code = $redis->hGet('smsCode', strval($mobile));
                if (!empty($code)) {
                    $content = $code;
                } else {
                    $_status = true;
                    $content = randCodes();
                }
                $sendInfo = "您的验证码为：" . $content . "。30分钟内有效，请勿向他人泄露验证码。";
            } else {
                $sendInfo = $content;
            }
            $BaseUrl = "http://request.ucpaas.com/sms-partner/access/nuochepai@126.com/sendsms";
            $body_json = array(
                "clientid" => "b020r8",
                "password" => md5("by123456"),
                "mobile" => $mobile,
                "smstype" => "0",
                "content" => '【E联车服】亲爱的用户:' . $sendInfo,
                "sendtime" => "",
                "extend" => "",
                "uid" => ""
            );
            $curl = new Curl();
            $data = $curl->post($BaseUrl, $body_json, 'json');
            if ($data) {
                if ($mark and $_status) {
                    # 发送验证码,存入redis
                    $redis->hSet('smsCode', strval($mobile), strval($content), '1800');
                }
                # 添加短信发送记录
                Db::table('log_sms')->data(array('biz_id' => $bizID, 'phone' => $mobile, 'sms_create' => date('Y-m-d H:i:s'), 'interface' => 1, 'content' => $content))->insert();
                return array('status' => true, 'code' => $content);
            } else {
                return array('status' => false, 'msg' => '发送失败');
            }
        } else {
            return array('status' => false, 'msg' => '手机号为空');
        }
    }

}
